<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/9/15
 * Time: 14:37
 */

class Admin extends ADM_Controller
{
    const DEFAULT_LIMIT = 20;

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 主程序
     */
    public function index()
    {
        $page = (int) $this->input->get('page');
        $page = empty($page) ? 1 : $page;

        $admin = $this->input->get('admin', '');
        $offset = ($page - 1) * self::DEFAULT_LIMIT;

        $this->load->model('admin/system/Admin_Model', 'admin');
        $res = $this->admin->getLists(array(
            'limit' => self::DEFAULT_LIMIT,
            'offset' => $offset,
            'admin' => $admin,
            'role' => $this->role,
            'baseId' => $this->baseId
        ));

        $newRoles = [];
        $roles = $this->admin->getRoles();
        if (!empty($roles)) foreach ($roles as $key => $role) {
            if ($role['id'] == 1) {
                continue;
            }
            $newRoles[] = $role;
        }

        $res = array_merge($res, array('roles' => $newRoles));
        $this->load->view(parent::TMP_DIR . '/admin/user.html', $res);
    }

    public function addAdmin()
    {
        $send['state'] = false;
        do {

            if ($this->input->method(true) !== 'POST') {
                $send['error'] = '非法请求';
                break;
            }

            $params = $this->input->post();
            if (empty($params['email'])
                || empty($params['nickName']) || empty($params['phone'])
                || empty($params['password'])) {

                $send['error'] = '参数错误';
                break;
            }

            $this->load->model('admin/system/Admin_Model', 'admin');
            $res = $this->admin->add($params);

            if ($res) {
                $send['state'] = true;
            } else {
                $send['error'] = '创建失败';
            }

        } while(false);

        $this->output->send($send);
    }

    public function editAdmin()
    {
        $send['state'] = false;
        do {
            if ($this->input->method(true) !== 'POST') {
                $send['error'] = '非法请求';
                break;
            }

            $params = $this->input->post();
            if (empty($params['id']) || empty($params['email'])
                || empty($params['nickName']) || empty($params['phone'])) {

                $send['error'] = '参数错误';
                break;
            }

            $this->load->model('admin/system/Admin_Model', 'admin');
            $res = $this->admin->edit($params);

            if ($res) {
                $send['state'] = true;
            } else {
                $send['error'] = '修改失败';
            }

        } while(false);

        $this->output->send($send);
    }

    /**
     * 头像上传
     */
    public function upLoad()
    {
        $send['state'] = false;
        do {
            $res = local($_FILES['file']);
            if (!$res) {
                $err = $this->up->error();
                $send['error'] = !empty($err) ? $err : '头像上传失败';
                break;
            }

            $res['fullPath'] = thumb($res['key'], 100, 100);

            $send['state'] = true;
            $send['data'] = $res;
        } while(false);

        $this->output->send($send);
    }

}